using System;
using System.Collections.Specialized;

namespace interpreter_base
{
    public class CompileEnvironment
	{
        public bool _Error_Resume = false;
        public bool _NoWaring = false;
        public NameValueCollection Variables = new NameValueCollection();
        public CC GetCCType(string vari)
        {
            ///decide string type
            if (vari[0] == char.Parse("\"") && vari[vari.Length - 1] == char.Parse("\""))
                return CC._variable;

            ///decide int type
            int tryInt;
            if (int.TryParse(vari, out tryInt))
                return CC._int;

            ///from variable decide type
            if (Variables[vari] != null)
            {
                if (int.TryParse(vari, out tryInt))
                {
                    return CC._int;
                }
                else
                {
                    return CC._string;
                }
            }
            if (!_NoWaring) Console.WriteLine("Interpreter Error:variable \"" + vari + "\" not define.");
            if (!_Error_Resume) Environment.Exit(-1);
            return CC._unknown;
        }
		public string GetValue(string vari)
		{
			CC cc = GetCCType(vari);
			
			switch (cc)
			{
			case CC._int:
				return vari;
			case CC._variable:
				return vari.Substring(1,vari.Length - 2);
			case CC._string:
				return Variables[vari];
			}
			
			if(cc == CC._variable)
			{
				return vari.Substring(1,vari.Length - 2);
			}
			return null;
		}
	}
	public enum CC
	{
		_function=-2,
		_variable=-1,
		_unknown=0,
		_int=1,
		_string=2,
	}
}
